window.onload=function () {
    if (localStorage.getItem('userHao') == null) {
        window.location = 'http://localhost:8080/future/loginpage';
    }
    var music = document.getElementsByClassName('music')[0];
    var musicForm = document.getElementsByClassName('musicForm')[0];
    music.onclick = function () {
        if (judge == true) {
            musicForm.style.display = 'block';
            judge = false;
            console.log();
        } else {
            musicForm.style.display = 'none';
            judge = true;
        }
    }

    // var find = document.getElementsByClassName('find')[0];
    // var search = document.getElementsByClassName('search')[0];
    var judge = true;
    // find.onclick = function () {
    //     if (judge == true) {
    //         console.log("aaaaaa");
    //         search.style.display = 'block';
    //         judge = false;
    //     } else {
    //         search.style.display = 'none';
    //         judge = true;
    //     }
    //
    //     console.log(judge);
    // }


    //查询搜索用户
    var input = document.getElementsByClassName('input')[0];//用户名的输入框
    var ssBtn = document.getElementsByClassName('ssBtn')[0];//搜索的按钮
    var Apage=document.getElementsByClassName('pageAll')[0];
    ssBtn.onclick = function () {
        $.ajax({
            url: 'http://localhost:8080/future/admin/queryUserByusername',
            method: "GET",
            data: {//参数
                userName: input.value,
            },
            success: function (data) {
                console.log(data);
                document.getElementsByClassName('content')[0].innerHTML = '';
                var a=data.length;
                Math.ceil(a / 10); //向上整除 4/3=2;
                console.log(  Math.ceil(a / 10));
                if(data.length===0){
                    alert("查无此信息！");
                    location.reload();
                }else {
                    for (var index = 0; index < data.length; index++) {
                        var element = data[index];
                        var i = index + 1;
                        document.getElementsByClassName("content")[0].innerHTML += "<tr class='found'>" + "<td class='find'>" + "<input class='choice' type='checkbox' name='ids'>" + "</td>" + "<td class='find'>" + i + "</td>" + "<td class='find'>" + element.userName + "</td>" + "<td class='find'>" + element.userHao + "</td>" + "<td class='find'>" + element.userGen + "</td>" + "<td class='find'>" + element.userEmail + "</td>" +
                            "<td class='find'>" + element.userLv + "</td>" + "<td class='find'>" + element.userVipBal + "</td>" +
                            "<td class='find'>" + "<button class='del'>删除</button>" + "<button class='amend'>更改</button>" + "</td>" + "</tr>"
                        console.log(data[0].userName);

                    }
                    Apage.innerHTML = Math.ceil(a / 10);
                }
            },
            error: function (err) {//请求失败，执行的回调函数

                console.log('错误');
            }
        })
    }

// 新增的弹窗
    var addUser = document.getElementsByClassName('addUser')[0];
    var add = document.getElementsByClassName('add')[0];
    var affirm = document.getElementsByClassName('affirm')[0];
    var cancel = document.getElementsByClassName('cancel')[0];
    var reset = document.getElementsByClassName('reset')[0];
    add.onclick = function () {
        addUser.style.display = 'block';
    }
    affirm.onclick = function () {
        addUser.style.display = 'none';
    }
    cancel.onclick = function () {
        addUser.style.display = 'none';
    }
    reset.onclick = function () {
        window.location.href = "http://localhost:8080/future/admin/queryAllUsers";//页面刷新
    }


    //新增一个用户
    var userName = document.getElementById('userName');
    var userEmail = document.getElementById('userEmail');
    var userHao = document.getElementById('userHao');
    var psw = document.getElementById('psw');
    var selectGen=document.getElementsByClassName('selectGen')[0];

    affirm.onclick = function () {
        $.ajax({
            url: 'http://localhost:8080/future/admin/adduser',
            method: 'POST',
            data: {
                userName: userName.value,
                userHao: userHao.value,
                userEmail: userEmail.value,
                userPwd: psw.value,
                userGen:selectGen.value,
            },
            success: function (data) {

                console.log(data);//控制台显示返回的数据
                location.reload();//添加成功则进行页面刷新

            },
            error: function (err) {//请求失败，执行的回调函数

                console.log('错误');
            }

        })
    }


    //删除用户信息

    var del = document.getElementsByClassName('del');
    for (var i = 0; i < del.length; i++) {
        del[i].onclick = function () {
            console.log(this.parentNode.parentNode.getAttribute("everid"));//获取当前行的id
            // $('.content').on('click', '.del', function () {//查询到要获取到的元素
            var flag = confirm("是否要删除该用户", "确定");
            // var xuHao = this.parentNode.parentNode.children[1].innerHTML;
            if (flag) {

                $.ajax({
                    url: "http://localhost:8080/future/admin/deleteUser",
                    type: "GET",
                    data: {
                        userId: this.parentNode.parentNode.getAttribute("everid"),
                    },
                    success: function (data) {
                        if (data.statusCode === 1) {
                            alert("删除成功");
                            window.location.reload();
                        } else {
                            alert("删除失败");

                            console.log(data.info);
                        }


                    },
                    error: function (err) {
                        console.log("错误");
                    },
                })
            }
        }


        //修改用户信息
        var Name = document.getElementsByClassName('userName')[0];
        var Email = document.getElementsByClassName('userEmail')[0];
        var userVIP = document.getElementsByClassName('userVIP')[0];
        var userLv = document.getElementsByClassName('selectLv')[0];
        var change = document.getElementsByClassName('amendUser')[0];
        var affirm_2 = document.getElementsByClassName('affirm_2')[0];
        var cancel_2 = document.getElementsByClassName('cancel_2')[0];

        $('.content').on('click', '.amend', function () {
            console.log(this.parentNode.parentNode.getAttribute("everid"));//获取当前行的id
            var userId = this.parentNode.parentNode.getAttribute("everid");
            change.style.display = 'block';
            Name.value = this.parentNode.parentNode.children[2].innerHTML;
            Email.value = this.parentNode.parentNode.children[5].innerHTML;
            userVIP.value = this.parentNode.parentNode.children[7].innerHTML;
                affirm_2.onclick = function () {
                        var flag=confirm("是否确认要修改当前用户的信息","确认");
                        if(flag){
                            alter();
                            change.style.display = 'none';
                        }else {
                            change.style.display = 'none';
                        }

                    }
                    cancel_2.onclick = function () {
                        change.style.display = 'none';
                    }
            var Gen = document.getElementsByClassName('Gen')[0];
               function alter() {
                   $.ajax({
                       url: 'http://localhost:8080/future/admin/updateUser',
                       method: 'GET',

                       data: {
                           userName: Name.value,
                           userEmail: Email.value,
                           userVipBal: userVIP.value,
                           userLv: userLv.value,
                           userId: userId,
                           userGen:Gen.value,
                       },
                       dataType: "json",
                       success: function (data) {
                           // document.getElementsByClassName('content')[0].innerHTML = "";
                           alert('修改成功');
                           location.reload();
                           console.log(data);
                       },
                       error: function (err) {//请求失败，执行的回调函数
                           console.log('错误');
                       }
                   })
               }
        })



            }
        }



        //头像处的列表的出现
        var headPhoto = document.getElementsByClassName('headPhoto')[0];
        var personage = document.getElementsByClassName('personage')[0];
        var xia2 = document.getElementsByClassName('xia2')[0];
        headPhoto.innerHTML = localStorage.getItem('userHao');
        headPhoto.onclick = function () {
            if (judge == true) {
                console.log("aaaaa");
                personage.style.height = '8%';
                judge = false;
            } else {
                personage.style.height = '0%';
                judge = true;
            }

        }


        //退出登录
        var back = document.getElementsByClassName('back')[0];
        back.onclick = function () {
            var flag = confirm("是否确定要退出登录", "确定");
            if (flag) {
                window.location.href = 'http://localhost:8080/future/loginpage';
                localStorage.removeItem('userHao');//清除保留的用户名
            }

    }
    //全选
    //点击获取下面全部的信息，再点取消
    var all=document.getElementById('all');
    function DoCheck()
    {
        var ch=document.getElementsByName("ids");
        if(document.getElementsByName("allChecked")[0].checked==true) {
            for(var i=0;i<ch.length;i++) {
                ch[i].checked=true;
            }
        }else{
            for(var i=0;i<ch.length;i++)
            {
                ch[i].checked=false;
            }
        }
    }
    all.onclick=DoCheck;




        //分页查询
        var previous=document.getElementsByClassName('previous')[0];
        var next=document.getElementsByClassName('next')[0];
        var page=1;
        var toPage=document.getElementsByClassName('toPage')[0];
        var pageAll=document.getElementsByClassName('pageAll')[0].innerHTML
        var first=document.getElementsByClassName('first')[0];
        var end=document.getElementsByClassName('end')[0];
        first.onclick=function () {
            location.href = 'http://localhost:8080/future/admin/queryAllUsers?page=1';
        }
        end.onclick=function (){
            location.href = 'http://localhost:8080/future/admin/queryAllUsers?page='+pageAll;
        }
        previous.onclick=function(){
            if(toPage.value<=1){
                alert("这已经是最前面一页！");
                return;
            }else{
                page=toPage.value-1;
                location.href = 'http://localhost:8080/future/admin/queryAllUsers?page='+page;
            }

        }
        next.onclick=function(){
            console.log(pageAll);
            var pagenum = pageAll;
            if(Number(toPage.value)>=pagenum){
                alert("没有下一页了！");
                return;
            }else{
                page=Number(toPage.value)+1;
                location.href = 'http://localhost:8080/future/admin/queryAllUsers?page='+page;
                console.log(page);
            }
        }

        //更改页码框中的值
        toPage.onkeydown=function(event) {
            var event = event;
            // console.log(event.keyCode)
            if (event.keyCode == 13) {
                location.href = 'http://localhost:8080/future/admin/queryAllUsers?page=' + Number(toPage.value);
            }
        }


//多个批量删除
            //点击批量删除按钮
            // var batch=document.getElementsByClassName('batch')[0];
            // batch.onclick=function (){
            //     //获取已经勾选的复选框
            //     console.log("aaaa")
            //     let checkedId=new Array();
            //     console.log("aaaa")//定义一个数组来保存已选中的value值
            //     $('input[name="ids"]:checked').each(function(){
            //         if(!isNaN($(this).val())){
            //             checkedId.push($(this).val());
            //         }else{
            //             console.log("拿不到");
            //         }
            //     });
            //     if(checkedId.length == 0){
            //         alert("请选择要删除的信息！");
            //         return false;
            //     }
            //     console.log("拿到的数组为："+checkedId);
            //     console.log("拿到的字符串为："+checkedId.toString());
                //进行批量删除操作
                // $.ajax({
                //     type:"POST",
                //     url:"",
                //     data:{"id":checkedId.toString()},
                //     success:function (data) {
                //         alert("请求返回的信息！");
                //         location.reload();      //重新刷新页面
                //     },
                //     error:function () {
                //         alert("请求失败！");
                //     }
                // });
            // }




$(function(){
    $(".batch").click(function(){
        //判断是否至少选择一项
        var checkedNum = $("input[name='ids']:checked").length;
        console.log(checkedNum);
        if(checkedNum==0){
            alert("请至少选择一项进行删除");
            return;
        }
        if(confirm("确定要删除所选项目？")){
            //这个数组存储要删除的数据的id
            var checkedList = [];
            //每一个被选中的name为ids的input标签
            $("input[name='ids']:checked").each(function(){
                checkedList.push(Number($(this).val()));//将id存入数组
                console.log(checkedList);

            });
            $.ajax({
                type:"GET",
                dataType:"json",
                contentType:"application/json",
                url:"http://localhost:8080/future/admin/BatchdeleteUsers",//不同页面只用更改action即可
                async:true,
                //数组的toString里的id是用","分隔开的，例如数组{"1","2","3"},同String后为"1,2,3"
                data:{
                    numbers:JSON.stringify(checkedList),
                },
                success:function(data){
                        alert("删除成功");
                        window.location.reload();
                },
                error:function(){
                    alert("错误");
                }
            })
        }
    })

})